import router from '@/router'
import Layout from '../views/Layout'

const actions = {
  login ({ commit }, value) {
    value.name && commit('LOGIN', value)
  },
  set_aside_menu ({ commit, getters }, value) {
    const list = [
      {
        path: '/',
        name: '首页',
        redirect: '/dashboard',
        component: Layout,
        children: [
          {
            path: 'dashboard',
            component: () => import('@/views/Dashboard')
          }
        ]
      },
      {
        path: '/menu1',
        name: '菜单一',
        component: Layout,
        children: [
          {
            path: '/menu1/submenu1',
            name: '子菜单一',
            component: () => import('@/views/Menu1/Submenu1')
          },
          {
            path: '/menu1/submenu2',
            name: '子菜单二',
            component: () => import('@/views/Menu1/Submenu2')
          }
        ]
      },
      {
        path: '/menu2',
        name: '菜单二',
        component: Layout,
        children: [
          {
            path: '/menu2/submenu1',
            name: '二子菜单一',
            component: () => import('@/views/Menu2/Submenu1')
          }
        ]
      }
    ]
    // 请求菜单数据
    return new Promise((resolve, reject) => {
      if (value) {
        // 动态添加或重新获取
        const list2 = list.filter(e => {
          let f = false
          getters.menuList.forEach(el => {
            if (e.name !== el.name) f = true
          })
          return f
        })
        resolve(list2)
        router.addRoutes([...list2])
        console.log(router)
        return
      }
      // 直接登录时 只显示2个路由
      resolve()
    }).then((res) => {
      commit('SET_ASIDE_MENU', res)
    }).catch(() => {
      console.log('获取菜单数据失败')
    })
  }
}
export default actions
